﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Car_Rental;
using Entity;
using System.Timers;
namespace Data.Car_Rental
{
    public class ProductPropertyTypeRepository : Car_RentalRepositoryBase, IProductPropertyTypeRepository
    {
        #region IRepository<ProductPropertyType> 成员

        public void Update(ProductPropertyType entity)
        {
            base.UpdateEntity(entity);
        }

        public void Update(IList<ProductPropertyType> list)
        {
            base.UpdateEntity(list.Cast<EntityBase>().ToList());
        }

        public void Insert(ProductPropertyType entity)
        {
            base.InsertEntity(entity);
        }

        public void Insert(IList<ProductPropertyType> list)
        {
            base.InsertEntity(list.Cast<EntityBase>().ToList());
        }

        public ProductPropertyType InsertGetIDENTITY(ProductPropertyType entity)
        {
            base.InsertEntity(entity);
            return this.Find(entity.PrimaryKey);
        }

        public void Delete(ProductPropertyType entity)
        {
            base.DeleteEntity(entity);
        }

        public void Delete(IList<ProductPropertyType> list)
        {
            base.DeleteEntity(list.Cast<EntityBase>().ToList());
        }

        public IQueryable<ProductPropertyType> GetModel()
        {
            return base.GetEntities<ProductPropertyType>();
        }

        public ProductPropertyType Find(params object[] keyValues)
        {
            return base.Find<ProductPropertyType>(keyValues);
        }

        #endregion

        #region 缓存
        static Timer sysTimer = new Timer(3600000);
        /// <summary>
        /// 数据实体
        /// </summary>
        public static volatile List<ProductPropertyType> Instance = null;
        // Lock对象，线程安全所用
        private static object syncRoot = new Object();
        /// <summary>
        /// 类构造方法
        /// </summary>
        static ProductPropertyTypeRepository()
        {
            ReLoad();
            sysTimer.AutoReset = true;
            sysTimer.Enabled = true;
            sysTimer.Elapsed += new ElapsedEventHandler(sysTimer_Elapsed);
            sysTimer.Start();
        }
        static void sysTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            ReLoad();
        }
        /// <summary>
        /// 重新加载数据结果集
        /// </summary>
        /// <returns></returns>
        public static List<ProductPropertyType> ReLoad()
        {
            lock (syncRoot)
            {
                Instance = new ProductPropertyTypeRepository().GetModel().ToList();
            }
            return Instance;
        }
        #endregion
    }
}
